package com.example.yin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.yin.common.R;
import com.example.yin.model.domain.Vip;
import com.example.yin.service.VipService;
import com.example.yin.mapper.VipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author Payment
 * @description 针对表【vip】的数据库操作Service实现
 * @createDate 2023-07-14 09:13:54
 */
@Service
public class VipServiceImpl extends ServiceImpl<VipMapper, Vip>
        implements VipService {
    @Autowired
    private VipMapper vipMapper;

    //将用户添加为VIP会员或延长VIP会员的截止时间。
    @Override
    public R addVip(int userid) {
        QueryWrapper<Vip> qw = new QueryWrapper<>();
        Calendar calendar = new GregorianCalendar();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Vip vip = new Vip();

        qw.eq("userid", userid);
        if (vipMapper.selectCount(qw) > 0) {
            vip = vipMapper.selectOne(qw);
            calendar.setTime(vipMapper.selectOne(qw).getDtime());
            calendar.add(Calendar.MONTH, 1);//time，是用户充值的月数
            vip.setDtime(calendar.getTime());
            vipMapper.update(vip, qw);
            return R.success("会员延长一个月，截止到" + sdf.format(vip.getDtime()));
        } else {
            vip.setCtime(new Date());
            calendar.setTime(vip.getCtime());
            calendar.add(Calendar.MONTH, 1);
            vip.setDtime(calendar.getTime());
            vip.setUserid(userid);
            vip.setFlag(1);
            vipMapper.insert(vip);
            return R.success("会员创建成功,截止到" + sdf.format(vip.getDtime()));
        }
    }

    //取消用户的VIP会员资格
    @Override
    public R delete(int userid) {
        QueryWrapper<Vip> qw = new QueryWrapper<>();
        qw.eq("userid", userid);
        if (vipMapper.delete(qw) > 0)
            return R.success("删除成功");
        else
            return R.error("删除失败");
    }

    //根据用户id获取VIP会员信息
    @Override
    public R vipOfId(int userid) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        QueryWrapper<Vip> qw = new QueryWrapper<>();
        qw.eq("userid", userid);
        Vip vip = vipMapper.selectOne(qw);
        if (vip == null) {
            return R.error("未查询到会员信息");
        } else if (ifOutOfDate(userid)) {
            return R.error("会员已过期");
        } else {
            return R.success("查询成功,截止到" + sdf.format(vip.getDtime()));
        }
    }

    //判断VIP会员是否过期
    private boolean ifOutOfDate(int userid) {
        QueryWrapper<Vip> qw = new QueryWrapper<>();
        qw.eq("userid", userid);
        Vip vip = vipMapper.selectOne(qw);
        Date now = new Date();
        if (now.compareTo(vip.getDtime()) > 0) {
            vipMapper.delete(qw);
            return true;
        } else return false;
    }
}




